# Maintainer (MSYS2): Michael Hansen <zrax0111@gmail.com>

_variant=-${KF5_VARIANT:-shared}
source "$(dirname ${BASH_SOURCE[0]})"/../mingw-w64-PKGBUILD-common/kde-frameworks5
_kde_f5_init_package "${_variant}" "breeze-icons"
pkgver=5.68.0
pkgrel=1
arch=('any')
pkgdesc="Breeze icon themes (mingw-w64-qt5${_namesuff})"
license=('LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-extra-cmake-modules>=${pkgver}")
depends=()
_kde_f5_add_depends "${_variant}" "${MINGW_PACKAGE_PREFIX}-qt5${_namesuff}"
groups=('kf5')
source=(#"https://download.kde.org/stable/frameworks/${pkgver%.*}/${_basename}-${pkgver}.tar.xz"
        ${_basename}-${pkgver}::git+https://github.com/KDE/breeze-icons.git#tag=v${pkgver})
sha256sums=('SKIP')
noextract=("${_basename}-${pkgver}.tar.xz")

prepare() {
  # bsdtar fails to extract this on Windows because it includes symlinks
  # before the targets.  Worse, it has symlinks to symlinks, so extracting
  # it twice isn't sufficient.  So we must keep trying until everything is
  # resolved :(.  However, we also enforce hard limit at 32 tries -- if it
  # doesn't work after that, it's time to just give up.
  #_tries=0
  #while ! /usr/bin/bsdtar xJf "${_basename}-${pkgver}.tar.xz" ; do
  #  _tries=$(( $_tries + 1 ))
  #  if (( $_tries == 32 )); then
  #    echo "Could not extract ${_basename}-${pkgver}.tar.xz after 32 tries.  Giving up!"
  #    exit 1
  #  fi
  #done

  mkdir -p build-${CARCH}${_variant}
}

build() {
  local -a extra_config
  cd build-${CARCH}${_variant}
  if [ "${_variant}" = "-static" ]; then
    extra_config+=( -DBUILD_SHARED_LIBS=NO )
  fi

  _kde_f5_build_env
  cmake ../${_basename}-${pkgver} \
    -DCMAKE_BUILD_TYPE=$(_kde_f5_CMAKE_BUILD_TYPE) \
    "${_kde_f5_KDE_INSTALL_DIRS[@]}" \
    -DBUILD_QCH=OFF \
    -DBUILD_TESTING=OFF \
    -DECM_DIR=${MINGW_PREFIX}/share/ECM \
    "${extra_config[@]}" \
    -G'MSYS Makefiles'
  make
}

package() {
  cd build-${CARCH}${_variant}
  make DESTDIR="${pkgdir}" install

  # KDE apps won't get an icon theme by default on Windows unless it's
  # provided via <QStandardPaths::GenericDataLocation>/icontheme.rcc
  # TODO: Make this better...
  _kde_f5_build_env
  install -Dm644 icons/breeze-icons.rcc "${pkgdir}/${QT5_PREFIX}"/bin/data/icontheme.rcc
}
